// pages/myorder/myorder.js
var app= getApp();//引用app.js
var commonURL=app.data.baseURL;
var commonURLXX = app.data.lastURL;
var commonURLYY = app.data.newURL;
const util = require("../../util/util.js");
var page=1;
Page({
  data:{
    "navTab":["全部","待付款","待发货","待收货","已收货"],
    "currentNavtab":"0",
    "ctnt0":[],
    "ctnt1":[],
    "ctnt2":[],
    "ctnt3":[],
    "ctnt4":[]
  },
  onLoad:function(options){
  },
  switchTab: function(e){
    var that=this;
    page=1;
    this.setData({
      currentNavtab: e.currentTarget.dataset.idx
    });
    var item=e.currentTarget.dataset.idx;
    console.log(item)
    var status="-1";
    switch (that.data.currentNavtab){
      case 0:
        status="-1";break;
      case 1:
        status="0";break;
      case 2:
        status="8";break;
      case 3:
        status="2";break;
      case 4:
        status="3";break;        
    }
    wx.request({
      url: commonURLYY+'op=order_list', 
      data: {
        page:page,
        pagesize:"10",
        openid:app.data.openid,
        status:status
      },
      header: {
          'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res.data)
        var data=res.data.list;
        if(data.length<1){
          return false;
        }
        wx.showToast({
          title: '订单加载中...',
          icon: 'loading',
          duration: 700
        })
        var arr=[];
        if(data.length>=1){
          for(var key in data){
            if (parseInt(data.g_id)<=0){

            }else{
              var obj={};
              obj.id=data[key].id;
              obj.tuan_id=data[key].tuan_id;
              obj.orderNum=data[key].orderno;
              obj.zhuangtai=that.zhuangtai(data[key].status);
              obj.freight=data[key].freight;
              obj.sumPrice=data[key].pay_price;
              obj.name=data[key].goodsname;
              obj.num=data[key].gnum;
              obj.price=data[key].goodsprice;
              obj.lists_guige=data[key].optionname;
              obj.collect = data[key].collect;
              arr.push(obj);
            }
          }
          if (that.data.currentNavtab=="0"){
              that.setData({
                ctnt0:arr
              })
          } else if (that.data.currentNavtab=="1"){
            that.setData({
              ctnt1:arr
            })
          } else if (that.data.currentNavtab=="2"){
            that.setData({
              ctnt2:arr
            })
          } else if (that.data.currentNavtab=="3"){
            that.setData({
              ctnt3:arr
            })
          } else if (that.data.currentNavtab=="4"){
            that.setData({
              ctnt4:arr
            })
          }
        }
      }
    })
  },
  gotoPj:function(e){
    wx.setStorage({
      key: "pjGoods",
      data: this.data.ctnt4[e.currentTarget.dataset.index],
      success:function(res){
        wx.navigateTo({
          url: '../appraisal/appraisal',
        })
      }
    })    
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    var that=this;
    // that.getOrderList('-1');
    wx.request({
      url: commonURLYY+'op=order_list', 
      data: {
        openid:app.data.openid,
        status:"-1"
      },
      header: {
          'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res.data)
         wx.showToast({
          title: '订单加载中...',
          icon: 'loading',
          duration: 700
        })
        var data=res.data.list;
        var arr=[];
        if(data.length>1){
          for(var key in data){
            var obj={};
            obj.g_id=data[key].g_id;
            obj.id=data[key].id;
            obj.tuan_id=data[key].tuan_id;
            obj.orderNum=data[key].orderno;
            obj.zhuangtai=that.zhuangtai(data[key].status);
            obj.freight=data[key].freight;
            obj.sumPrice=data[key].pay_price;
            obj.name=data[key].goodsname;
            obj.num=data[key].gnum;
            obj.price=data[key].goodsprice;
            obj.lists_guige=data[key].optionname;
            obj.collect = data[key].collect;
            arr.push(obj);
          }
        }
        that.setData({
          "ctnt0":arr
        });
      }
    })
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  cancel:function(e){
    var that=this;
    var out_trade_no = e.currentTarget.dataset.outtradeno;
    var nonce_str= that.createNonceStr();
    var stringA = "appid=" + app.data.appid + "&mch_id=" + app.data.mch_id + "&nonce_str=" + nonce_str + "&out_trade_no=" + out_trade_no;
    var stringSignTemp = stringA + "&key=" + app.data.paykey;
    var sign=that.MD5(stringSignTemp);
    var formData = "<xml>"
    formData += "<appid>" + app.data.appid + "</appid>" //appid
    formData += "<mch_id>" + app.data.mch_id + "</mch_id>" //商户号 attach
    formData += "<nonce_str>" + nonce_str + "</nonce_str>" //随机字符串，不长于32位。
    formData += "<out_trade_no>" + out_trade_no + "</out_trade_no>"
    formData += "<sign>" + sign + "</sign>"
    formData += "</xml>"
    //取消订单
    wx.showModal({
      title: '确定取消订单吗',
      success: function(res) {
        if (res.confirm) {
          //向微信后台取消订单
          wx.request({
            url: 'https://api.mch.weixin.qq.com/pay/closeorder ',
            data: formData,
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
              console.log(res)
              //向后台取消订单
              wx.request({
                url: commonURL + 'op=cancal',
                data: {
                  orderno: e.currentTarget.dataset.outtradeno
                },
                header: {
                  'content-type': 'application/json'
                },
                success: function (res) {
                  console.log(res)
                  if (res.data.status == "1") {
                    wx.showToast({
                      title: '订单已成功取消',
                      icon: 'success',
                      duration: 4000
                    })
                    page = 1;
                    var item = that.data.currentNavtab;
                    var status = "";
                   that.onShow();
                  } else {
                    wx.showToast({
                      title: '订单取消失败',
                      icon: 'loading',
                      duration: 4000
                    })
                  }
                }
              })
            },
            fail: function (res) {
              wx.showToast({
                title: '订单取消失败',
                icon: 'loading',
                duration: 4000
              })
            }
          })
        }
      }
    })
  },
  getOrderList: function (status){
    var that=this;
    wx.request({
      url: commonURLYY + 'op=order_list',
      data: {
        openid: app.data.openid,
        status: status,
        page: 1,
        pagesize: "10",
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.list.length < 1) {
          return false;
        }
        console.log(res.data)
        wx.showToast({
          title: '订单加载中...',
          icon: 'loading',
          duration: 700
        })
        var data = res.data.list;
        var arr = [];
        if (data.length > 1) {
          for (var key in data) {
            var obj = {};
            obj.g_id = data[key].g_id;
            obj.id = data[key].id;
            obj.tuan_id = data[key].tuan_id;
            obj.orderNum = data[key].orderno;
            obj.zhuangtai = that.zhuangtai(data[key].status);
            obj.freight = data[key].freight;
            obj.sumPrice = data[key].pay_price;
            obj.name = data[key].goodsname;
            obj.num = data[key].gnum;
            obj.price = data[key].goodsprice;
            obj.lists_guige = data[key].optionname;
            obj.collect = data[key].collect;
            arr.push(obj);
          }
        }
        that.setData({
          "ctnt0": arr
        });
      }
    })
  },
  zhuangtai:function(status){
    switch (status){
      case "0"://等待 付款******  
        return "待付款"; break;
      case "1"://支付成功  
        return "已支付"; break;
      case "8"://等待商家发货    0 1 8
        return "待发货"; break;
      case "2"://订单已发货  0 1 8 2
        return "待收货"; break;  
      case "3"://订单完成  0 1 8 2 3
        return "已收货"; break;
      case "10"://等待退款 0 1 10
        return "待退款"; break;  
      case "4"://订单已退款  0 1 10 4
        return "已退款"; break;
      case "7"://订单已退款  0 1 10 7
        return "已退款"; break;
      case "6"://部分退款  0 1 8 2 6
        return "部分退款"; break;
      case "9"://订单已取消  0 9****
        return "已取消"; break;   
      case "5"://订单已取消  0 9****
        return "部分签收"; break;      
    }
  },
  gotoGroupDetail:function(e){
    console.log(e)
    wx.navigateTo({
      url: '../mygroup_detail/mygroup_detail?tuan_id='+e.currentTarget.dataset.id+'&g_id='+e.currentTarget.dataset.g_id
    })
  },
  gotoOrderDetail:function(e){
    console.log(e)
    wx.navigateTo({
      url: '../myorder_detail/myorder_detail?orderid=' + e.currentTarget.dataset.id + '&orderno=' + e.currentTarget.dataset.orderno
    })
  },
  verify:function(e){
    console.log(e)
    var orderno = e.currentTarget.dataset.id;
    var areaid = e.currentTarget.dataset.areaid;
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        console.log(res)
        wx.request({
          url: commonURL +'op=scan',
          data: {
            openid: app.data.openid,
            orderno: orderno,
            uniacid: app.data.GZHid,
            tuan_openid: util.parseURL(res.result).mid
          },
          success:function(res){
            if (res.data.status==1){
              wx.navigateTo({
                url: '../check/check?orderno=' + orderno,
              })
            }else{
              wx.showToast({
                title: '暂无核销权限',
              })
            }
          }
        })
      }
    })
  },
  upper: function () {
    wx.showNavigationBarLoading()
    this.refresh();
    console.log("upper");
    setTimeout(function(){wx.hideNavigationBarLoading();wx.stopPullDownRefresh();}, 2000);
  },
  remember: function (e) {
    console.log(e)
    var info = e.currentTarget.dataset.info
    wx.setStorage({
      key: 'selectOrder',
      data: info,
      success: function () {
        wx.navigateTo({
          url: '../appraisal/appraisal',
        })
      }
    })
  },
  lower: function (e) {
    wx.showNavigationBarLoading();
    var that = this;
    setTimeout(function(){wx.hideNavigationBarLoading();that.nextLoad();}, 1000);
    console.log("lower")
  },
  ajaxOrderList:function(that,page){
    
  },
  // 上拉加载回调接口
  onReachBottom: function () {
      var that=this;
      var status="-1";
      switch (that.data.currentNavtab){
        case "0":
          status="-1";break;
        case "1":
          status="0";break;
        case "2":
          status="8";break;
        case "3":
          status="2";break;
        case "4":
          status="3";break;        
      }
      page++;
      wx.request({
        url: commonURLYY+'op=order_list', 
        data: {
          page:page,
          pagesize:"10",
          openid:app.data.openid,
          status:status
        },
        header: {
            'content-type': 'application/json'
        },
        success: function(res) {
          console.log(res.data)
          var data=res.data.list;
          if(data.length<1){
            wx.showToast({
              title: '全部订单加载完成',
              icon: 'success',
              duration: 1000
            })
            return false;
          }
          wx.showToast({
            title: '订单加载中...',
            icon: 'loading',
            duration: 1000
          })
          var arr=[];
          if(data.length>=1){
            for(var key in data){
              var obj={};
              obj.g_id = data[key].g_id;
              obj.id=data[key].id;
              obj.tuan_id=data[key].tuan_id;
              obj.orderNum=data[key].orderno;
              obj.zhuangtai=that.zhuangtai(data[key].status);
              obj.freight=data[key].freight;
              obj.sumPrice=data[key].pay_price;
              obj.name=data[key].goodsname;
              obj.num=data[key].gnum;
              obj.price=data[key].goodsprice;
              obj.lists_guige=data[key].optionname;
              obj.collect = data[key].collect;
              arr.push(obj);
            }
          }
      // 我们用total和count来控制分页，total代表已请求数据的总数，count代表每次请求的个数。
      // 上拉时需把total在原来的基础上加上count，代表从count条后的数据开始请求。
          that.setData({
            "ctnt0":that.data.ctnt0.concat(arr)
          });
        }
      })
  },
  orderSubmit:function(e){
    var that=this;
    if (that.data.ctnt0[e.currentTarget.dataset.index].collect.length == 0 && parseInt(that.data.ctnt0[e.currentTarget.dataset.index].g_id)==0){
      wx.showToast({
        title: '该订单无效',
      })
      return;
    }
    if (parseInt(that.data.ctnt0[e.currentTarget.dataset.index].g_id==0)){
      var goodsname = that.data.ctnt0[e.currentTarget.dataset.index].collect[0].goodsname;
      var many_limit = that.data.ctnt0[e.currentTarget.dataset.index].collect[0].many_limit;
      var history_limit = that.data.ctnt0[e.currentTarget.dataset.index].collect[0].history_limit;
      var kucun = that.data.ctnt0[e.currentTarget.dataset.index].collect[0].gnum;
      var isshow = that.data.ctnt0[e.currentTarget.dataset.index].collect[0].isshow;
    }else{
      var goodsname = that.data.ctnt0[e.currentTarget.dataset.index].goodsname;
      var many_limit = that.data.ctnt0[e.currentTarget.dataset.index].many_limit;
      var history_limit = that.data.ctnt0[e.currentTarget.dataset.index].history_limit;
      var kucun = that.data.ctnt0[e.currentTarget.dataset.index].gnum;
      var isshow = that.data.ctnt0[e.currentTarget.dataset.index].isshow;
    }
    if ( parseInt(many_limit) == 0) {
      if (99999 < parseInt(history_limit)) {
        var pay = goodsname + '该商品已达到总限购数量' + many_limit;
        wx.showToast({
          title: '' + pay,
          icon: 'none',
          duration: 4000
        })
        return
      }
    } else {
      if (parseInt(many_limit) <= parseInt(history_limit)) {
        var pay = goodsname + '该商品已达到总限购数量333333' + many_limit;
        wx.showToast({
          title: '' + pay,
          icon: 'none',
          duration: 4000
        })
        return
      }
    }
    if (kucun == 0 || isshow == '3') {
      var pay = goodsname + '已售罄，请先删除该产品再去支付';
      wx.showToast({
        title: '' + pay,
        icon: 'none',
        duration: 4000
      })
      return
    }
    if (isshow == 2) {
     var pay = goodsname + '已下架，请先删除该产品再去支付';
      wx.showToast({
        title: '' + pay,
        icon: 'none',
        duration: 4000
      })
      return
    }
    //提交订单,弹出模态框
    wx.showModal({
      title: '确认支付？',
      success: function(res) {
          if (res.confirm) {
          var total_fee=parseFloat(e.currentTarget.dataset.totalfee)*100;//总价格,以分为单位，不能有小数点
          var nonce_str=that.createNonceStr();
          var out_trade_no=e.currentTarget.dataset.outtradeno+ that.createRand();
          var _orderno = e.currentTarget.dataset.outtradeno;
            wx.request({//后台返回数据  统一下单数据
              url: 'https://min.lexiangpingou.cn/addons/lexiangpingou/lxapi/xcx_pay.php?op=wxpay',
              data: {
                appid: app.data.appid,
                mch_id: app.data.mch_id,
                body: app.data.body + e.currentTarget.dataset.name,
                openid: app.data.openid,
                out_trade_no: out_trade_no,
                total_fee: total_fee.toFixed(0),
                paykey: app.data.paykey
              },
            "success": function(res) {
              console.log(res.data);
              var data=res.data;
               wx.requestPayment({//发起微信支付接口-------------
                 'timeStamp': data.timeStamp,
                 'nonceStr': data.nonceStr,
                 'package': data.package,
                'signType': 'MD5',
                'paySign': data.paySign,
                'success':function(res){
                    console.log(res);
                    wx.showToast({
                      title: '支付成功',
                      icon: 'success',
                      duration: 3000
                    })
                    that.orderquery(_orderno,out_trade_no,that);//获取微信支付订单号并发送给后台
                },
                'fail':function(res){
                    console.log(res);
                    wx.showToast({
                      title: "支付失败",
                      icon: 'loading',
                      duration: 4000
                    })
                    that.orderquery(_orderno,out_trade_no, that);//获取微信支付订单号并发送给后台
                }
               })
            },
            fail:function(res){
              console.log(res);
              wx.showToast({
                title: '支付失败',
                icon: 'loading',
                duration: 2000
              })
              that.onShow()
            }
          })
        }
      }
    })
  },
  createNonceStr:function(){
    var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/  
    var maxPos = chars.length;  
    var pwd = '';  
    for (var i = 0; i < 32; i++) {  
      pwd += chars.charAt(Math.floor(Math.random() * maxPos));  
    }
    return pwd
  },
  createRand:function(){//产生5位随机数字，用于拼接订单号
    var chars = '0123456789';    
    var maxPos = chars.length;  
    var pwd = '';  
    for (var i = 0; i < 4; i++) {  
      pwd += chars.charAt(Math.floor(Math.random() * maxPos));  
    }
    return pwd
  },
  split:function(xml,node_name){
    var tmp = xml.split("<" + node_name + ">") 
    var _tmp = tmp[1].split("</" + node_name + ">") 
    return _tmp[0].split("<![CDATA[")[1].split("]]")[0] 
  },
  MD5:function(a){//MD5加密代码
      var hexcase=1;var mmdd=rstr2hex(rstr_md5(str2rstr_utf8(a)));return mmdd;function hex_hmac_md5(a,b){return rstr2hex(rstr_hmac_md5(str2rstr_utf8(a),str2rstr_utf8(b)))}function md5_vm_test(){return hex_md5("abc").toLowerCase()=="900150983cd24fb0d6963f7d28e17f72"}function rstr_md5(a){return binl2rstr(binl_md5(rstr2binl(a),a.length*8))}function rstr_hmac_md5(c,f){var e=rstr2binl(c);if(e.length>16){e=binl_md5(e,c.length*8)}var a=Array(16),d=Array(16);for(var b=0;b<16;b++){a[b]=e[b]^909522486;d[b]=e[b]^1549556828}var g=binl_md5(a.concat(rstr2binl(f)),512+f.length*8);return binl2rstr(binl_md5(d.concat(g),512+128))}function rstr2hex(c){try{hexcase}catch(g){hexcase=0}var f=hexcase?"0123456789ABCDEF":"0123456789abcdef";var b="";var a;for(var d=0;d<c.length;d++){a=c.charCodeAt(d);b+=f.charAt((a>>>4)&15)+f.charAt(a&15)}return b}function str2rstr_utf8(c){var b="";var d=-1;var a,e;while(++d<c.length){a=c.charCodeAt(d);e=d+1<c.length?c.charCodeAt(d+1):0;if(55296<=a&&a<=56319&&56320<=e&&e<=57343){a=65536+((a&1023)<<10)+(e&1023);d++}if(a<=127){b+=String.fromCharCode(a)}else{if(a<=2047){b+=String.fromCharCode(192|((a>>>6)&31),128|(a&63))}else{if(a<=65535){b+=String.fromCharCode(224|((a>>>12)&15),128|((a>>>6)&63),128|(a&63))}else{if(a<=2097151){b+=String.fromCharCode(240|((a>>>18)&7),128|((a>>>12)&63),128|((a>>>6)&63),128|(a&63))}}}}}return b}function rstr2binl(b){var a=Array(b.length>>2);for(var c=0;c<a.length;c++){a[c]=0}for(var c=0;c<b.length*8;c+=8){a[c>>5]|=(b.charCodeAt(c/8)&255)<<(c%32)}return a}function binl2rstr(b){var a="";for(var c=0;c<b.length*32;c+=8){a+=String.fromCharCode((b[c>>5]>>>(c%32))&255)}return a}function binl_md5(p,k){p[k>>5]|=128<<((k)%32);p[(((k+64)>>>9)<<4)+14]=k;var o=1732584193;var n=-271733879;var m=-1732584194;var l=271733878;for(var g=0;g<p.length;g+=16){var j=o;var h=n;var f=m;var e=l;o=md5_ff(o,n,m,l,p[g+0],7,-680876936);l=md5_ff(l,o,n,m,p[g+1],12,-389564586);m=md5_ff(m,l,o,n,p[g+2],17,606105819);n=md5_ff(n,m,l,o,p[g+3],22,-1044525330);o=md5_ff(o,n,m,l,p[g+4],7,-176418897);l=md5_ff(l,o,n,m,p[g+5],12,1200080426);m=md5_ff(m,l,o,n,p[g+6],17,-1473231341);n=md5_ff(n,m,l,o,p[g+7],22,-45705983);o=md5_ff(o,n,m,l,p[g+8],7,1770035416);l=md5_ff(l,o,n,m,p[g+9],12,-1958414417);m=md5_ff(m,l,o,n,p[g+10],17,-42063);n=md5_ff(n,m,l,o,p[g+11],22,-1990404162);o=md5_ff(o,n,m,l,p[g+12],7,1804603682);l=md5_ff(l,o,n,m,p[g+13],12,-40341101);m=md5_ff(m,l,o,n,p[g+14],17,-1502002290);n=md5_ff(n,m,l,o,p[g+15],22,1236535329);o=md5_gg(o,n,m,l,p[g+1],5,-165796510);l=md5_gg(l,o,n,m,p[g+6],9,-1069501632);m=md5_gg(m,l,o,n,p[g+11],14,643717713);n=md5_gg(n,m,l,o,p[g+0],20,-373897302);o=md5_gg(o,n,m,l,p[g+5],5,-701558691);l=md5_gg(l,o,n,m,p[g+10],9,38016083);m=md5_gg(m,l,o,n,p[g+15],14,-660478335);n=md5_gg(n,m,l,o,p[g+4],20,-405537848);o=md5_gg(o,n,m,l,p[g+9],5,568446438);l=md5_gg(l,o,n,m,p[g+14],9,-1019803690);m=md5_gg(m,l,o,n,p[g+3],14,-187363961);n=md5_gg(n,m,l,o,p[g+8],20,1163531501);o=md5_gg(o,n,m,l,p[g+13],5,-1444681467);l=md5_gg(l,o,n,m,p[g+2],9,-51403784);m=md5_gg(m,l,o,n,p[g+7],14,1735328473);n=md5_gg(n,m,l,o,p[g+12],20,-1926607734);o=md5_hh(o,n,m,l,p[g+5],4,-378558);l=md5_hh(l,o,n,m,p[g+8],11,-2022574463);m=md5_hh(m,l,o,n,p[g+11],16,1839030562);n=md5_hh(n,m,l,o,p[g+14],23,-35309556);o=md5_hh(o,n,m,l,p[g+1],4,-1530992060);l=md5_hh(l,o,n,m,p[g+4],11,1272893353);m=md5_hh(m,l,o,n,p[g+7],16,-155497632);n=md5_hh(n,m,l,o,p[g+10],23,-1094730640);o=md5_hh(o,n,m,l,p[g+13],4,681279174);l=md5_hh(l,o,n,m,p[g+0],11,-358537222);m=md5_hh(m,l,o,n,p[g+3],16,-722521979);n=md5_hh(n,m,l,o,p[g+6],23,76029189);o=md5_hh(o,n,m,l,p[g+9],4,-640364487);l=md5_hh(l,o,n,m,p[g+12],11,-421815835);m=md5_hh(m,l,o,n,p[g+15],16,530742520);n=md5_hh(n,m,l,o,p[g+2],23,-995338651);o=md5_ii(o,n,m,l,p[g+0],6,-198630844);l=md5_ii(l,o,n,m,p[g+7],10,1126891415);m=md5_ii(m,l,o,n,p[g+14],15,-1416354905);n=md5_ii(n,m,l,o,p[g+5],21,-57434055);o=md5_ii(o,n,m,l,p[g+12],6,1700485571);l=md5_ii(l,o,n,m,p[g+3],10,-1894986606);m=md5_ii(m,l,o,n,p[g+10],15,-1051523);n=md5_ii(n,m,l,o,p[g+1],21,-2054922799);o=md5_ii(o,n,m,l,p[g+8],6,1873313359);l=md5_ii(l,o,n,m,p[g+15],10,-30611744);m=md5_ii(m,l,o,n,p[g+6],15,-1560198380);n=md5_ii(n,m,l,o,p[g+13],21,1309151649);o=md5_ii(o,n,m,l,p[g+4],6,-145523070);l=md5_ii(l,o,n,m,p[g+11],10,-1120210379);m=md5_ii(m,l,o,n,p[g+2],15,718787259);n=md5_ii(n,m,l,o,p[g+9],21,-343485551);o=safe_add(o,j);n=safe_add(n,h);m=safe_add(m,f);l=safe_add(l,e)}return Array(o,n,m,l)}function md5_cmn(h,e,d,c,g,f){return safe_add(bit_rol(safe_add(safe_add(e,h),safe_add(c,f)),g),d)}function md5_ff(g,f,k,j,e,i,h){return md5_cmn((f&k)|((~f)&j),g,f,e,i,h)}function md5_gg(g,f,k,j,e,i,h){return md5_cmn((f&j)|(k&(~j)),g,f,e,i,h)}function md5_hh(g,f,k,j,e,i,h){return md5_cmn(f^k^j,g,f,e,i,h)}function md5_ii(g,f,k,j,e,i,h){return md5_cmn(k^(f|(~j)),g,f,e,i,h)}function safe_add(a,d){var c=(a&65535)+(d&65535);var b=(a>>16)+(d>>16)+(c>>16);return(b<<16)|(c&65535)}function bit_rol(a,b){return(a<<b)|(a>>>(32-b))};
      
  },
  orderquery: function (_orderno,out_trade_no,that){//查询订单，把“微信支付订单号”发给后台
    // var nonce_str=that.createNonceStr();
    //MD5加密代码
    // var stringA="appid="+app.data.appid+"&mch_id="+app.data.mch_id+"&nonce_str="+nonce_str+"&out_trade_no="+out_trade_no;
    // var stringSignTemp=stringA+"&key="+app.data.paykey; 
    // var sign=that.MD5(stringSignTemp);  
    // var dataxml="<xml><appid>"+app.data.appid+"</appid><mch_id>"+app.data.mch_id+"</mch_id><nonce_str>"+nonce_str+"</nonce_str><out_trade_no>"+out_trade_no+"</out_trade_no><sign>"+sign+"</sign></xml>";
    wx.request({
      url: 'https://min.lexiangpingou.cn/addons/lexiangpingou/lxapi/xcx_pay.php?op=wx_query', //商户可以通过查询订单接口主动查询订单状态，完成下一步的业务逻辑。 
      data: {
        appid: app.data.appid,
        mch_id: app.data.mch_id, 
        out_trade_no: out_trade_no,
        paykey: app.data.paykey
      },
      header: {
          'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res.data);
        var data=res.data;
        if (data.result_code != 'SUCCESS' && data.transaction_id!=''){
          wx.showToast({
            title: data.trade_state_desc,
            icon: 'loading',
            duration: 4000
          })
          return false;
        }
        // var transaction_id=that.split(res.data,"transaction_id");
        // var total_fee = parseInt(data.split("<total_fee>")[1].split("</total_fee>"))/100;
         wx.request({
           url: commonURLXX+'op=check_payresult', 
          data: {
            uniontid: out_trade_no,  //商户订单号
            orderno: _orderno,        //订单id
            transid:data.transaction_id,
            fee:Number(data.total_fee) * 0.01
          },
          header: {
              'content-type': 'application/json'
          },
          success: function(res) {
            console.log(res.data);
            if(res.data.status!="1"){return false;}
            if(!res.data.tuan_id){//判断是否是团购，跳转订单详情
              wx.redirectTo({url: '../myorder_detail/myorder_detail?orderid='+res.data.order_id});
            }else{//跳转团详情
              wx.redirectTo({url: '../mygroup_detail/mygroup_detail?goodsid='+res.data.order_id+'&tuan_id='+res.data.tuan_id});
            }
          }
        })
      }
    })
  }
  })